<template>
  <div class="main-wrap">
    <div class="scroll-box">
      <!-- 搜索框 -->
      <transition name="van-slide-down">
        <div v-show="loaded" class="search-wrapper">
          <van-field class="search-field" size="normal" v-model="keywords" center clearable placeholder="请输入全名搜索座位信息">
            <template #left-icon>
              <van-icon name="search" color="#ba1a2d" size="24" />
            </template>
            <template #button>
              <van-button size="small" round color="#ba1a2d" class="search-button" @click="onSearch"> 搜索 </van-button>
            </template>
          </van-field>
        </div>
      </transition>

      <!-- 行程 -->
      <div class="mt20 flex-cc" v-if="schedules.length > 0">
        <Ticket v-for="schedule in schedules" :key="schedule.subject" :schedule="schedule" @click="onShowSeat(schedule.orderDetailId, schedule.personId)" />
      </div>
      <div v-else class="empty-wrap">
        <div ref="container"></div>
      </div>
    </div>
  </div>

  <!-- 座位省略图 -->
  <van-popup v-model:show="showSeat" position="bottom" round closeable close-icon="close" :style="{ height: '60%' }">
    <div class="seat-wrap" v-if="seatGrid.length > 0">
      <div class="podium">舞台</div>
      <div v-for="(row, rIdx) in seatGrid" :key="rIdx" class="seat-row">
        <div
          v-for="seat in row"
          :key="seat.colIndex"
          :class="{ 'seat-item': seat.type === LayoutType.GRID, 'round-item': seat.type === LayoutType.ROUND, active: seat.active }"
          :data-uuid="seat.uuid"
        ></div>
      </div>
    </div>
  </van-popup>
</template>

<script setup lang="ts">
import { LayoutType, type LayoutElement, type SeatSchedule } from "@/types/banquet";
import { onMounted, ref } from "vue";
import { buildSeatGrid, fetchSeat, fetchSeatSchedule } from ".";
import { animateCSS } from "@/utils/animation";
import { showDialog } from "vant";
import Ticket from "./components/ticket.vue";
import seatEmptyJson from "@/assets/seat_empty.json";
import lottie from "lottie-web";

// lottie动画提示
const container = ref(null);
// 加载完成
const loaded = ref(false);
// 人员全名
const keywords = ref("");
// 异常信息
const errMsg = ref("请输入全名搜索座位信息");
// 网格座位二维数组
const seatGrid = ref<LayoutElement[][]>([]);
// 展示座位图
const showSeat = ref(false);
// 人员行程
const schedules = ref<SeatSchedule[]>([]);

/**
 * 查询座位信息
 */
const onSearch = async () => {
  const name = keywords.value.trim();
  if (name.length === 0) {
    animateCSS(".search-wrapper", "shakeX");
    schedules.value = [];
    return;
  }
  const data = await fetchSeatSchedule(name);
  if (data.length === 0) {
    showDialog({ message: "抱歉没找到相关人员信息" });
  }
  schedules.value = data;
};

/**
 * 打开预览图
 * @param orderDetailId
 * @param personId
 */
const onShowSeat = async (orderDetailId: number, personId: number) => {
  const data = await fetchSeat(orderDetailId, personId);
  showSeat.value = true;
  seatGrid.value = buildSeatGrid(data.elements, data.layout);
};

onMounted(() => {
  loaded.value = true;

  if (container.value) {
    lottie.loadAnimation({
      container: container.value,
      renderer: "svg",
      loop: true,
      autoplay: true,
      animationData: seatEmptyJson,
    });
  }
});
</script>

<style lang="less" scoped>
.main-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.scroll-box {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 10px;
}

.search-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;

  .search-field {
    border-radius: 999px;
    width: 100%;
    padding: 5px 8px;
    box-shadow: 10px 10px 30px rgba(187, 26, 45, 0.3);
  }

  .search-button {
    padding: 0 14px;
  }
}

.empty-wrap {
  margin-top: 30px;
  text-align: center;
}

.seat-wrap {
  background-color: #c2c2c2;
  width: 80%;
  margin: 50px auto 10px auto;
  padding: 10px;
  border-radius: 5px;

  .podium {
    background-color: #d9d9d9;
    color: #444451;
    height: 40px;
    width: 50%;
    margin: 10px auto 20px auto;
    text-align: center;
    line-height: 40px;
    box-shadow: 0 3px 10px rgba(255, 255, 255, 0.7);
  }

  .seat-row {
    display: flex;
    justify-content: space-around;

    .seat-item {
      display: inline-block;
      color: #fff;
      background-color: #f0fbf7;
      height: 12px;
      width: 15px;
      margin: 3px;
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      transition: transform 0.3s ease-in-out;

      &.active {
        background-color: var(--ui-THEME);
      }
    }

    .round-item {
      display: inline-block;
      color: #fff;
      background-color: #f0fbf7;
      height: 15px;
      width: 15px;
      margin: 3px;
      border-radius: 10px;
      transition: transform 0.3s ease-in-out;

      &.active {
        background-color: var(--ui-THEME);
      }
    }
  }
}
</style>
